<?php

class ImageUploadForm extends sfForm {

  public function configure() {
	$this->setNameFormat();


	for ($i = 1; $i <= $this->getPicturesAmount(); ++$i) {
	  $required = false;
	  if ($i == 1) {
		$required = true;
	  }
	  $this->widgetSchema['picture' . $i] = new sfWidgetFormInputFile();
	  $this->validatorSchema['picture' . $i] = new sfValidatorFile(
					  array('required' => $required,
						  'max_size' => 2097152, // 2MB
						  'mime_types' => 'web_images'));
	}
	
	 $this->widgetSchema['name'] = new sfWidgetFormInput();
     $this->widgetSchema['lastname'] = new sfWidgetFormInput();
     $this->widgetSchema['phone'] = new sfWidgetFormInput();
     $this->widgetSchema['email'] = new sfWidgetFormInput();
	 $this->widgetSchema['captcha']= new sfWidgetFormInput(array(), array());
	 
	 $this->validatorSchema['name'] = new sfValidatorString(array('required' => true,'min_length' => 0, 'max_length' => 100));
	 $this->validatorSchema['lastname'] = new sfValidatorString(array('required' => true,'min_length' => 0, 'max_length' => 100));
	 $this->validatorSchema['phone'] = new sfValidatorString(array('required' => true,'min_length' => 0, 'max_length' => 100));
	 $this->validatorSchema['email'] = new sfValidatorEmail(array('required' => true,'min_length' => 0, 'max_length' => 100));
	 $this->validatorSchema['captcha'] =  new sfCaptchaGDValidator(array('length' => 4), array('length' => 'Wrong captcha', 'invalid' => 'Wrong captcha'));
  }

  public function getPicturesAmount() {
	return 4;
  }

  public function processForm(sfWebRequest $request) {
	$this->bind($request->getParameter($this->getName()), 
			$request->getFiles($this->getName()));

	if (!$this->isValid()) {
	  return false;
	}
	$files = array();
	for ($i = 1; $i <= $this->getPicturesAmount(); ++$i) {
	  if (!$this->getValue('picture' . $i)) {
		break;
	  }

	  $file = $this->getValue('picture' . $i);
	  $fileName = $this->getRandomPictureName();
	  $file->save(sfConfig::get('sf_web_dir').'/uploads/banner/'.$fileName);
	  
	  $bannerId = $this->createBanner($fileName);
	  
	  $files[$bannerId] = $file;
	}
	return $files;
  }

  public function getRandomPictureName() {
	return sha1(time().rand(1,1000)) . '.jpg';
  }
  
  public function createBanner($file) {
	$bg = Doctrine::getTable('BannerGroup')->findOneByType('frontend_gallery');

	$banner = new Banner();
	$banner->setName('Kasutaja: '.$this->getValue('name').' '.$this->getValue('lastname'));
	$banner->setContent('Kasutaja: '.$this->getValue('name').' '.$this->getValue('lastname').
						"\nEmail: ".$this->getValue('email').
						"\nTelefon: ".$this->getValue('phone'));
	$banner->setFile('/banner/'.$file);
	$banner->setType('default');
	$banner->setIsActive(0);
	$banner->setBannerGroup($bg);

	$banner->save();	
	return $banner->getId();
  }

  public function setNameFormat() {
	$this->widgetSchema->setNameFormat('attachment[%s]');
  }

  public function getErrors() {
	sfProjectConfiguration::getActive()->loadHelpers(array('I18N'));
	
	$result = array();

	foreach ($this->getErrorSchema() as $index => $val) {
	  $result[] = __($index).' : '.__($val->__toString());
	}
	return $result;
  }

}